package com.qxt.prize.mappers;

import com.qxt.prize.models.Prize;
import org.springframework.jdbc.core.RowMapper;
import top.moirae.core.AbstractOption;
import top.moirae.security.mappers.UserMapper;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Objects;

public class PrizeMapper {
    public static final RowMapper<Prize> DETAIL = (rs, index) -> {
        Prize entity = new Prize();
        entity.setId(rs.getLong("id"));
        entity.setName(rs.getString("name"));
        entity.setDescription(rs.getString("description"));
        entity.setValue(rs.getObject("value", BigDecimal.class));
        entity.setQuantity(rs.getObject("quantity", Long.class));
        entity.setType(Prize.Type.valueOf(rs.getString("type")));
        entity.setCover(rs.getString("cover"));
        entity.setPublishAt(AbstractOption.apply(rs.getTimestamp("publish_at")).map(Timestamp::toLocalDateTime).orNull());
        entity.setCreateAt(AbstractOption.apply(rs.getTimestamp("create_at")).map(Timestamp::toLocalDateTime).orNull());
        entity.setCreateBy(UserMapper.dynamicEssentialColumnsPackage("create_by", "create_by_name").mapRow(rs, index));
        return entity;
    };

    public static RowMapper<Prize> dynamicEssentialColumnsPackage(String id, String name) {
        return (rs, rowNum) -> {
            Prize obj = null;
            if (Objects.nonNull(rs.getObject(id, Long.class))) {
                obj = new Prize();
                obj.setId(rs.getObject(id, Long.class));
                obj.setName(rs.getString(name));
            }
            return obj;
        };
    }
    public static RowMapper<Prize> dynamicEssentialColumnsPackage(String id, String name, String value, String type) {
        return (rs, rowNum) -> {
            Prize obj = null;
            if (Objects.nonNull(rs.getObject(id, Long.class))) {
                obj = new Prize();
                obj.setId(rs.getObject(id, Long.class));
                obj.setName(rs.getString(name));
                obj.setValue(rs.getObject(value, BigDecimal.class));
                obj.setType(Prize.Type.valueOf(rs.getString(type)));
            }
            return obj;
        };
    }

    public static RowMapper<Prize> dynamicColumnsPackage(String id, String name, String value, String description, String quantity, String type, String cover) {
        return (rs, rowNum) -> {
            Prize obj = null;
            if (Objects.nonNull(rs.getObject(id, Long.class))) {
                obj = new Prize();
                obj.setId(rs.getObject(id, Long.class));
                obj.setName(rs.getString(name));
                obj.setDescription(rs.getString(description));
                obj.setValue(rs.getObject(value, BigDecimal.class));
                obj.setQuantity(rs.getObject(quantity, Long.class));
                obj.setType(Prize.Type.valueOf(rs.getString(type)));
                obj.setCover(rs.getString(cover));
            }
            return obj;
        };
    }
}

